<?php 
	include("includes/connect_db.php");  

	$penerima = $_POST['penerima'];
	$temp = $_POST['temp'];
	$status = array();
	$angkatan = array();
	$mk = array();
	$pa = array();
	$agama = array();
	$entitas = array();

	$sql_lihat_host_akademik = "SELECT * FROM tb_host WHERE db = 'db_seo_akademik'";
	$query_lihat_host_akademik = mysql_query($sql_lihat_host_akademik);
	$lihat_host_akademik = mysql_fetch_array($query_lihat_host_akademik);

	$konMhs = mysqli_connect($lihat_host_akademik['host'], $lihat_host_akademik['username'], $lihat_host_akademik['password']) or die("Connection Error: " . mysqli_error());
	mysqli_select_db($konMhs, $lihat_host_akademik['db']) or die("Error conecting to db.");

	$sql_lihat_host_pegawai = "SELECT * FROM tb_host WHERE db = 'db_seo_kepegawaian'";
	$query_lihat_host_pegawai = mysql_query($sql_lihat_host_pegawai);
	$lihat_host_pegawai = mysql_fetch_array($query_lihat_host_pegawai);

	$konPeg = mysqli_connect($lihat_host_pegawai['host'], $lihat_host_pegawai['username'], $lihat_host_pegawai['password']) or die("Connection Error: " . mysqli_error());
	mysqli_select_db($konPeg, $lihat_host_pegawai['db']) or die("Error conecting to db.");

	$i = 0;
	foreach ($penerima as $filter) {
		if ($_POST['tipe'] == "mahasiswa") {
			switch ($filter[0]) {
				case 'status':
					$jumlah = count($status);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($status as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($status, $filter[1]);
						}
					} else{
						array_push($status, $filter[1]);
					}
					break;
				
				case 'angkatan':
					$jumlah = count($angkatan);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($angkatan as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($angkatan, $filter[1]);
						}
					} else{
						array_push($angkatan, $filter[1]);
					}
					break;

				case 'mk':
					$jumlah = count($mk);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($mk as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($mk, $filter[1]);
						}
					} else{
						array_push($mk, $filter[1]);
					}
					break;

				case 'pa':
					$jumlah = count($pa);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($pa as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($pa, $filter[1]);
						}
					} else{
						array_push($pa, $filter[1]);
					}
					break;

				case 'agama':
					$jumlah = count($agama);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($agama as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($agama, $filter[1]);
						}
					} else{
						array_push($agama, $filter[1]);
					}
					break;
			}
		} else {
			switch ($filter[0]) {
				case 'entitas':
					$jumlah = count($entitas);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($entitas as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($entitas, $filter[1]);
						}
					} else{
						array_push($entitas, $filter[1]);
					}
					break;
				
				case 'status':
					$jumlah = count($status);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($status as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($status, $filter[1]);
						}
					} else{
						array_push($status, $filter[1]);
					}
					break;

				case 'agama':
					$jumlah = count($agama);
					if ($jumlah > 0) {
						$val = 0;
						foreach ($agama as $cek) {
							if ($cek == $filter[1]) {
								$val = 1;
							}
						}
						if ($val != 1) {
							array_push($agama, $filter[1]);
						}
					} else{
						array_push($agama, $filter[1]);
					}
					break;
			}
		}
	}

	if ($_POST['tipe'] == "mahasiswa") {
		if ($temp == "view") {
			if ((count($status) > 0) OR (count($angkatan) > 0) OR (count($pa) > 0) OR (count($agama) > 0)) {
				$query_view = "SELECT id_mhs AS ID, nim AS NIM, nama AS NAMA, hp AS TELEPON FROM tb_mhs WHERE";
			} else {
				$query_view = "";
			}
			$cek_jumlah = 0;

			$jumlah = count($status);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				if ($jumlah == 1) {
					$query_view .= " STATUS_TERAKHIR = '".$status[0]."'";
				} else {
					$i = 1;
					$query_view .= " (";
					foreach ($status as $arr_status) {
						$query_view .= "STATUS_TERAKHIR = '".$arr_status."'";
						if ($i < $jumlah) {
							$query_view .= " OR ";
						}
						$i++;
					}
					$query_view .= ")";
				}
			}

			$jumlah = count($angkatan);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				if (count($status) > 0) {
					$query_view .= " AND";
				}
				if ($jumlah == 1) {
					$query_view .= " ID_ANGKATAN = '".$angkatan[0]."'";
				} else {
					$i = 1;
					$query_view .= " (";
					foreach ($angkatan as $arr_angkatan) {
						$query_view .= "ID_ANGKATAN = '".$arr_angkatan."'";
						if ($i < $jumlah) {
							$query_view .= " OR ";
						}
						$i++;
					}
					$query_view .= ")";
				}
			}

			$jumlah = count($pa);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				if ((count($status) > 0) OR (count($angkatan) > 0)) {
					$query_view .= " AND";
				}
				if ($jumlah == 1) {
					$query_view .= " pa = '".$pa[0]."'";
				} else {
					$i = 1;
					$query_view .= " (";
					foreach ($pa as $arr_pa) {
						$query_view .= "pa = '".$arr_pa."'";
						if ($i < $jumlah) {
							$query_view .= " OR ";
						}
						$i++;
					}
					$query_view .= ")";
				}
			}

			$jumlah = count($agama);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				if ((count($status) > 0) OR (count($angkatan) > 0) OR (count($pa) > 0)) {
					$query_view .= " AND";
				}
				if ($jumlah == 1) {
					$query_view .= " ID_AGAMA = '".$agama[0]."'";
				} else {
					$i = 1;
					$query_view .= " (";
					foreach ($agama as $arr_agama) {
						$query_view .= "ID_AGAMA = '".$arr_agama."'";
						if ($i < $jumlah) {
							$query_view .= " OR ";
						}
						$i++;
					}
					$query_view .= ")";
				}
			}

			$jumlah = count($mk);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				$query_temp = "";
				if ($jumlah == 1) {
					$query_temp .= " ID_PENAWARAN = '".$mk[0]."'";
				} else {
					$i = 1;
					$query_temp .= " (";
					foreach ($mk as $arr_mk) {
						$query_temp .= "ID_PENAWARAN = '".$arr_mk."'";
						if ($i < $jumlah) {
							$query_temp .= " OR ";
						}
						$i++;
					}
					$query_temp .= ")";
				}

				$i = 1;
				$sql = "SELECT * FROM tb_angkatan";
				$query = mysqli_multi_query($konMhs, $sql);
			    $data = mysqli_store_result($konMhs);
				while ($angkatan = mysqli_fetch_array($data)) {
					if ($i == 1) {
						$query_view_2 = "SELECT id_mhs AS ID FROM tb_krs_".$angkatan['angkatan']." WHERE ".$query_temp;
					} else {
						$query_view_2 .= " UNION SELECT id_mhs AS ID FROM tb_krs_".$angkatan['angkatan']." WHERE ".$query_temp;
					}
					$i++;
				}


			}

			if ((count($status) > 0) OR (count($angkatan) > 0) OR (count($pa) > 0) OR (count($agama) > 0)) {
				// $query_view .= " ORDER BY NIM";
				if (count($mk) == 0) {
					$query_final = $query_view." ORDER BY NIM";
				} else {
					$query_final = $query_view." AND id_mhs IN (".$query_view_2.") ORDER BY NIM";
				}
			} elseif (count($mk) > 0) {
				$query_final = "SELECT id_mhs AS ID, nim AS NIM, nama AS NAMA, hp AS TELEPON FROM tb_mhs WHERE id_mhs IN (".$query_view_2.") ORDER BY NIM";
			}
		}
	} else {
		if ($temp == "view") {
			$query_view = "SELECT nip_baru AS ID, nama_lengkap AS NAMA, telp AS TELEPON FROM tb_master_pegawai WHERE";
			$cek_jumlah = 0;

			$jumlah = count($entitas);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				if ($jumlah == 1) {
					$query_view .= " tanda = '".$entitas[0]."'";
				} else {
					$i = 1;
					$query_view .= " (";
					foreach ($entitas as $arr_entitas) {
						$query_view .= "tanda = '".$arr_entitas."'";
						if ($i < $jumlah) {
							$query_view .= " OR ";
						}
						$i++;
					}
					$query_view .= ")";
				}
			}

			$jumlah = count($agama);
			if ($jumlah != 0) {
				$cek_jumlah = 1;
				if (count($entitas) > 0) {
					$query_view .= " AND";
				}
				if ($jumlah == 1) {
					$query_view .= " ID_MASTER_AGAMA = '".$agama[0]."'";
				} else {
					$i = 1;
					$query_view .= " (";
					foreach ($agama as $arr_agama) {
						$query_view .= "ID_MASTER_AGAMA = '".$arr_agama."'";
						if ($i < $jumlah) {
							$query_view .= " OR ";
						}
						$i++;
					}
					$query_view .= ")";
				}
			}
			$query_view .= " ORDER BY ID";
		}
	}
?>

<STYLE TYPE="text/css">
	.modal-header{
		background-color: #08c;
		color: #FFF;
	}
	#form-modal{
		margin: 0 0 0 -400px;
		width: 800px;
	}
	.modal-body{
		max-height: 500px;
		overflow: auto;
	}
	.modal-footer{
		background-color: #ccc;
		color: black;
		font-weight: bold;
		text-align: right;
	}
	.table-header{
		text-align: center;
		font-weight: bold;
	}
</STYLE>

<div id="form-modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="icon-remove"></i></button>
        <h3 id="myModalLabel">
            Penerima Broadcast
        </h3>
    </div>
    <div class="modal-body">
    	<table class="table table-bordered">
    		<tr class="table-header">
    			<td width="5%">No</td>
    			<td width="15%">ID</td>
    			<td width="50%">Nama</td>
    			<td width="30%">Telepon</td>
    		</tr>
	        <?php  
	        	if ($cek_jumlah == 1) {
		        	$i = 1;
		        	if ($_POST['tipe'] == "mahasiswa") {
			        	$query = mysqli_multi_query($konMhs, $query_final);
			        	$data = mysqli_store_result($konMhs);
			        	while ($result = mysqli_fetch_array($data)) {
	        ?>
	        <tr>
	        	<td><?php echo $i; ?></td>
	        	<td><?php echo $result['NIM'] ?></td>
	        	<td><?php echo $result['NAMA'] ?></td>
	        	<td><?php echo $result['TELEPON'] ?></td>
	        </tr>
	        <?php
	        				$i++;
	        			}
	        		} else {
	        			$query = mysqli_multi_query($konPeg, $query_view);
			        	$data = mysqli_store_result($konPeg);
			        	while ($result = mysqli_fetch_array($data)) {			
			?>
			<tr>
	        	<td><?php echo $i; ?></td>
	        	<td><?php echo $result['ID'] ?></td>
	        	<td><?php echo $result['NAMA'] ?></td>
	        	<td><?php echo $result['TELEPON'] ?></td>
	        </tr>
			<?php
						$i++;
	        			}
	        		}
	        	}
	        ?>
    	</table>
    </div>
    <div class="modal-footer">
    	<?php  
    		if ($_POST['tipe'] == "mahasiswa") {
	    		$query = mysqli_multi_query($konMhs, $query_final);
	    		$data = mysqli_store_result($konMhs);
	    		$jumlah = mysqli_num_rows($data);
    		} else {
    			$query = mysqli_multi_query($konPeg, $query_view);
	    		$data = mysqli_store_result($konPeg);
	    		$jumlah = mysqli_num_rows($data);
    		}
    		echo $jumlah." Data Ditampilkan";
    	?>
    </div>
</div>

<SCRIPT TYPE="text/javascript">
	$(document).ready(function() {
		$('#form-modal').modal('show');
	});
</SCRIPT>